package qlsl.androiddesign.popupwindow.subwindow;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import qlsl.androiddesign.activity.baseactivity.BaseActivity;
import qlsl.androiddesign.adapter.subadapter.ProvinceGridAdapter;
import qlsl.androiddesign.popupwindow.basewindow.PopupWindow;

/**
 * 品牌PopupWindow<br/>
 */
@SuppressLint("ViewConstructor")
public class ProvincePopupWindow extends PopupWindow {

	private GridView gridView;

	public ProvincePopupWindow(BaseActivity activity) {
		super(activity);
		initView();
		setPopupWindowAttribute();
		initData();
	}

	@SuppressLint("InflateParams")
	private void initView() {
		LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rootView = inflater.inflate(R.layout.window_brand, null);
		setContentView(rootView);
		gridView = (GridView) rootView.findViewById(R.id.gridView);
	}

	private void setPopupWindowAttribute() {
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);
		setAnimationStyle(R.style.AnimTop);
		ColorDrawable colorDrawable = new ColorDrawable(
				activity.getResources().getColor(R.color.bg_common_content_view));
		setBackgroundDrawable(colorDrawable);
		setOutsideTouchable(true);
	}

	private void initData() {
		setGridViewData();
	}

	private void setGridViewData() {
		String[] provinces = new String[] { "不限", "北京", "天津", "上海", "重庆", "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏", "浙江",
				"安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾", "内蒙古",
				"广西", "西藏", "宁夏", "新疆", "香港", "澳门" };
		List<String> list = new ArrayList<String>();
		for (String text : provinces) {
			list.add(text);
		}
		ProvinceGridAdapter adapter = new ProvinceGridAdapter(activity, list, gridView);
		gridView.setAdapter(adapter);
	}

	public String getSelectItem() {
		ProvinceGridAdapter adapter = (ProvinceGridAdapter) gridView.getAdapter();
		return adapter.getItem(adapter.getSelectPosition());
	}
}
